/**
 * 用户详情对话框
 */
var UserInfoDlg = {
    data: {
        deptId: "",
        deptName: ""
    }
};

layui.use(['layer', 'form', 'admin', 'laydate', 'ax', 'formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var formSelects = layui.formSelects;


    //获取框架信息
    var ajax = new $ax(Feng.ctxPath + "/frame/getOneFrameInfo?frameId=" + Feng.getUrlParam("frameId"));
    var frameResult = ajax.start();

    var html = '';
    html += '<div class="layui-inline layui-col-md12">';
    html += '   <input name="frameId" value='+ frameResult.data.frameId + ' type="hidden" />';
    html += '   <label class="layui-form-label">框架名称<span style="color: red;">*</span></label>';
    html += '   <div class="layui-input-block">';
    if(frameResult.data.postStatus =="ENABLE") {
    html += '       <input name="frameName" placeholder="请输入名称" value=' + frameResult.data.frameName + ' type="text" class="layui-input" lay-verify="required" required disabled style="background:#f1eaea"/>';
    }else{
    html += '       <input name="frameName" placeholder="请输入名称" value='+frameResult.data.frameName+' type="text" class="layui-input" lay-verify="required" required />';
    }
    html += '   </div>';
    html += '</div>';
    html += '<input name="postStatus" value= '+ frameResult.data.postStatus + ' type="hidden" />';
    html += '<div class="layui-inline layui-col-md12">';
    html += '   <label class="layui-form-label">状态</label>';
    html += '   <div class="layui-input-block">';
    if (frameResult.data.status =="ENABLE"){
    html += '       <input type="radio" name="status" value="ENABLE" title="公开" checked/>';
    html += '       <input type="radio" name="status" value="DISABLE" title="关闭"/>';
    }else {
    html += '       <input type="radio" name="status" value="ENABLE" title="公开" />';
    html += '       <input type="radio" name="status" value="DISABLE" title="关闭" checked/>';
    }
    html += '   </div>';
    html += '</div>';

    html += '<div class="layui-inline layui-col-md12">';
    html += '   <label class="layui-form-label">描述</label>';
    html += '   <div class="layui-input-block">';
    if(frameResult.data.postStatus =="ENABLE") {
    html += '       <textarea placeholder="请输入内容" class="layui-textarea" name="frameDescription" disabled style="background:#f1eaea">' + frameResult.data.frameDescription + '</textarea>';
    }else {
    html += '       <textarea placeholder="请输入内容" class="layui-textarea" name="frameDescription">' + frameResult.data.frameDescription + '</textarea>';
    }
    html += '   </div>';
    html += '</div>';

    $("#frameContent").append(html);
    //因为input type="radio"在页面显示不出来，所以加了下面的语句，更新全部
    form.render();


    //获取框架项信息
    var ajax = new $ax(Feng.ctxPath + "/frameitem/getOneFrameitemInfo?frameId=" + Feng.getUrlParam("frameId"));
    var frameitemResult = ajax.start();
    var num = frameitemResult.data.length-1;
    for (var index in frameitemResult.data) {
        //遍历框架项内容并显示，以及删除

        var html = '';
        html += '<div id="abc'+ index +'">';
        html += '    <input name="frameitemId[]" value='+frameitemResult.data[index].frameitemId+' type="hidden" />';
        html += '    <div class="layui-inline layui-col-md4">';
        html += '        <label class="layui-form-label">开始时间<span style="color: red;">*</span></label>';
        html += '        <div class="layui-input-block">';
        if(frameResult.data.postStatus =="ENABLE") {
        html += '            <input id="testabc" type="text" name="startTime[]" value="' + frameitemResult.data[index].startTime + '" class="layui-input date-icon datetime" autocomplete="off" lay-verify="required" required disabled/>';
        }else{
        html += '            <input id="testabc" type="text" name="startTime[]" value="'+ frameitemResult.data[index].startTime +'" class="layui-input date-icon datetime" autocomplete="off" lay-verify="required" required />';
        }
        html += '        </div>';
        html += '    </div>';
        html += '    <div class="layui-inline layui-col-md4">';
        html += '        <label class="layui-form-label">结束时间<span style="color: red;">*</span></label>';
        html += '        <div class="layui-input-block">';
        if(frameResult.data.postStatus =="ENABLE"){
        html += '            <input id="testabc" type="text" name="endTime[]" value="'+ frameitemResult.data[index].endTime +'" class="layui-input date-icon datetime" autocomplete="off" lay-verify="required" required disabled/>';
        }else {
        html += '            <input id="testabc" type="text" name="endTime[]" value="'+ frameitemResult.data[index].endTime +'" class="layui-input date-icon datetime" autocomplete="off" lay-verify="required" required />';
        }
        html += '        </div>';
        html += '    </div>';
        html += '    <div class="layui-inline layui-col-md10">';
        html += '        <label class="layui-form-label">备注</label>';
        html += '        <div class="layui-input-block">';
        if(frameResult.data.postStatus =="ENABLE"){
        html += '            <input name="remark[]" value="'+ frameitemResult.data[index].remark +'" placeholder="请输入备注" type="text" class="layui-input" disabled style="background:#f1eaea"/>';
        }else {
        html += '            <input name="remark[]" value="'+ frameitemResult.data[index].remark +'" placeholder="请输入备注" type="text" class="layui-input"  />';
        }
        html += '        </div>';
        html += '    </div>';
        html += '    <input name="sort[]" value="' + frameitemResult.data[index].sort + '" type="hidden" class="layui-input"   />';
        html += '</div>';

        $("#frameitemNUm").append(html);


    }
    //如果框架未发布，显示添加按钮
    if (frameResult.data.postStatus =="DISABLE"){
        var addButton = '<button type="button" id="addFrameitem" class="layui-btn" >添加</button>';
        $("#menu").append(addButton);
    }

    //如果框架项超过两个且没有发布则显示删除按钮
    if (num>=1 && frameResult.data.postStatus =="DISABLE"){
        var deleteButton = '<button type="button" id="deleteFrameitem" class="layui-btn ">删除最后一条</button>';
        $("#menu").append(deleteButton);
    }


    //点击删除最后一条
    $(document).on('click','#deleteFrameitem',function (data) {
        $("#abc"+num).remove();
        num--;
        if (num ==0){
            $("#deleteFrameitem").remove();
        }
    });


    /**
     * 点击添加按钮
     */
    $(document).on('click','#addFrameitem',function () {
        num++;
        var html = '';

        html += '<div id="abc'+num+'">';
        html += '    <div class="layui-inline layui-col-md4">';
        html += '        <label class="layui-form-label">开始时间<span style="color: red;">*</span></label>';
        html += '        <div class="layui-input-block">';
        html += '            <input id="testabc" type="text" name="startTime[]" class="layui-input date-icon datetime" autocomplete="off" lay-verify="required" required />'
        html += '        </div>';
        html += '    </div>';
        html += '    <div class="layui-inline layui-col-md4">';
        html += '        <label class="layui-form-label">结束时间<span style="color: red;">*</span></label>';
        html += '        <div class="layui-input-block">';
        html += '            <input id="testabc" type="text" name="endTime[]" class="layui-input date-icon datetime" autocomplete="off" lay-verify="required" required />';
        html += '        </div>';
        html += '    </div>';
        html += '    <div class="layui-inline layui-col-md10">';
        html += '        <label class="layui-form-label">备注</label>';
        html += '        <div class="layui-input-block">';
        html += '            <input name="remark[]" placeholder="请输入备注" type="text" class="layui-input"  />';
        html += '        </div>';
        html += '    </div>';
        html += '    <input name="sort[]" value="0" type="hidden" class="layui-input"   />';
        html += '</div>';

        $("#frameitemNUm").append(html);
        //如果超过两条则显示删除
        if (num ==1){
            var deletebut = '<button type="button" id="deleteFrameitem" class="layui-btn ">删除最后一条</button>'
            $("#menu").append(deletebut);
        }
        form.render();
        timeAdd();
    });



    // 渲染时间选择框
    function timeAdd(){
        lay('.datetime').each(function() {
            //时间选择器
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'time'
                ,format: 'HH:mm'
                ,done: function (value, date, endDate) {
                    console.log(value); //得到日期生成的值，如：2017-08-18
                }
            });
        });
    }

    timeAdd();

    //表单提交事件:发布
    form.on('submit(btnSubmitRelease)', function (data) {

        var fields=$(data.form).serialize();
        var alllist = fields.split("&")

        var ajax = new $ax(Feng.ctxPath + "/frame/frame_addAndRelese", function (data) {
            Feng.success("发布成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            // 关闭当前页面
            // var index = parent.layer.getFrameIndex(window.name);
            // parent.layer.close(index);

            //刷新父页面
            parent.location.reload();
            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("发布失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        // alert(data.field.PAGEMESSAGE)
        var ajax = new $ax(Feng.ctxPath + "/frame/frame_editadd",data, function (data) {
            Feng.success("修改成功！");
            //传给上个页面，刷新table用
            // admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
        });
        //刷新父页面
        // parent.location.reload();
        ajax.set(data.field);
        ajax.start();

        // 添加 return false 可成功跳转页面
        return false;
    });
});

